<template>
  <div>
    <user v-if="active === '3'"/>
    <query v-if="active === '1' && custType === '3'"/>
    <train v-if="active === '2' && custType === '3'"/>
    <tenant-management v-if="active === '1' && custType === '5'" :title="$t('home.t1')"/>
    <user-management v-if="active === '1' && custType === '2'" :title="$t('home.t2')"/>
    <database-management v-if="active === '2' && custType === '2'"/>
    <van-tabbar v-model="active" @change="onChange" style="box-shadow: 0 0 25px #cac6c6;">
      <van-tabbar-item name="1" icon="home-o" v-if="custType === '5'">{{ $t("home.t1") }}</van-tabbar-item>
      <van-tabbar-item name="1" icon="home-o" v-if="custType === '2'">{{ $t("home.t2") }}</van-tabbar-item>
      <van-tabbar-item name="1" icon="home-o" v-if="custType === '3'">{{ $t("home.t3") }}</van-tabbar-item>
      <van-tabbar-item name="2" icon="points" v-if="custType === '2'">{{ $t("home.t4") }}</van-tabbar-item>
      <van-tabbar-item name="2" icon="points" v-if="custType === '3'">{{ $t("home.t5") }}</van-tabbar-item>
      <van-tabbar-item name="3" icon="setting-o">{{ $t("home.t6") }}</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import { Tabbar, TabbarItem } from 'vant';
import tenantManagement from '../tenantManagement'
import userManagement from '../userManagement'
import databaseManagement from '../databaseManagement'
import query from '../query'
import train from '../train'
import user from '../user'
import {getUserInfo} from "../../api/admin";
import {getSessionItem, setSessionItem} from "../../utils/storage";
import configJs from "../../config";
let Base64 = require('js-base64').Base64

export default {
  components: {
    user,
    query,
    train,
    tenantManagement,
    userManagement,
    databaseManagement,
    [Tabbar.name]: Tabbar,
    [TabbarItem.name]: TabbarItem,
  },

  data() {
    return {
      active: '1',
      custType: '',
    };
  },

  created: function () {
    let self = this
    let userInfoStr = getSessionItem('userInfo')
    const active = getSessionItem('active')
    self.active = active || self.active
    if (userInfoStr === null) {
      getUserInfo(self, null).then((response) => {
        if (response.data.code === configJs.errCode.errOk || response.data.code.startsWith('0')) {
          self.custType = response.data.body.custType
          userInfoStr = JSON.stringify(response.data.body)
          if (configJs.storage.storageEncode === 'true') {
            userInfoStr = Base64.encode(userInfoStr)
          }
          //  console.log(userInfoStr)
          setSessionItem('userInfo', userInfoStr)
        }
      }).catch(function (error) {
        console.log(error)
      })
    } else {
      // console.log('        222' + userInfoStr)
      if (configJs.storage.storageEncode === 'true') {
        userInfoStr = Base64.decode(userInfoStr)
      }
      let userInfo = JSON.parse(userInfoStr)
      console.log('custType:', userInfo.custType)
      self.custType = userInfo.custType
    }
  },

  methods: {
    onChange(val) {
      setSessionItem('active', val)
    }
  }
};
</script>

<style lang="less">
.user {
  &-poster {
    width: 100%;
    height: 53vw;
    display: block;
  }

  &-group {
    margin-bottom: 15px;
  }

  &-links {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;

    .van-icon {
      display: block;
      font-size: 24px;
    }
  }
}
</style>
